package com.wertop.viid.common.utils;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit;
import com.wertop.viid.common.constant.Constants;

/**
 * @Description 缓存工具类
 * @Author yempty
 * @Date 2020/6/1 16:57
 */
public class LocalCacheUtil {

    /**
     * 创建缓存，默认5分钟过期
     */
    public static TimedCache<String, String> timedCache = CacheUtil.newTimedCache(Constants.TOKEN_EXPIRE);

    static {
        // 启动定时清理任务
        timedCache.schedulePrune(Constants.TOKEN_CLEAN_DELAY);
    }

    public static void put(String key, String value) {
        timedCache.put(key, value);
    }

    public static void put(String key, String value, Integer expire) {
        timedCache.put(key, value, DateUnit.SECOND.getMillis() * expire);
    }

    /**
     * 禁止延迟缓存 isUpdateLastAccess = false
     *
     * @param key
     * @param isUpdateLastAccess
     */
    public static String get(String key, boolean isUpdateLastAccess) {
        return timedCache.get(key, isUpdateLastAccess);
    }

    public static String get(String key) {
        return timedCache.get(key);
    }

    public static void remove(String key) {
        timedCache.remove(key);
    }

    public static void clear() {
        timedCache.clear();
    }
}
